package name.panitz.mobile;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import java.util.*;

public class GameMidlet extends MIDlet{
  Timer timer = new Timer();
  Vector mos=new Vector();

    Canvas myCanvas = new MyCanvas();
    class MyCanvas extends Canvas{
      public void paint(Graphics g){

      g.setColor(255,255,255);	  
      g.fillRect(0,0,getWidth(),getHeight());

      for (Enumeration e = mos.elements();e.hasMoreElements();){
        MovableObject mo=(MovableObject)e.nextElement();

	mo.paintMe(g);
      }
    }
  };

  TimerTask myRefreshTask = new TimerTask(){
    public void run(){
      doOnTic();
      myCanvas.repaint();
    }
  };

  public void doOnTic(){
    for (Enumeration e = mos.elements();e.hasMoreElements();){
      MovableObject mo=(MovableObject)e.nextElement();	
      mo.move();
    }
  }

  public GameMidlet(){
    initDisplay();
  }

  void initDisplay(){
    Display.getDisplay(this).setCurrent(myCanvas);

    myCanvas.addCommand(new Command("Exit", Command.EXIT, 0)); 
    myCanvas.setCommandListener(new CommandListener(){
      public void commandAction(Command c, Displayable s){ 
        if (c.getCommandType() == Command.EXIT) 
          notifyDestroyed(); 
       } 
    });
  }

  public void startApp(){timer.schedule(myRefreshTask,0,50);}

  public void pauseApp(){timer.cancel();} 

  public void destroyApp(boolean unconditional){}
}

